package com.example.reagent_management.room.database

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import com.example.reagent_management.room.dao.HistoryDao
import com.example.reagent_management.room.dao.MessageDao
import com.example.reagent_management.room.dao.ReagentInDao
import com.example.reagent_management.room.dao.StoreDao
import com.example.reagent_management.room.entity.HistoryInfo
import com.example.reagent_management.room.entity.ReagentInformation
import com.example.reagent_management.room.entity.ReagentOperate
import com.example.reagent_management.ui.message.pojo.Message

const val DATABASE_NAME = "reagent_manage.db"

@Database(
    entities = [ReagentOperate::class, ReagentInformation::class, HistoryInfo::class,Message::class],
    version = 1,
    exportSchema = false
)
//复杂类型转换器
@TypeConverters(Converters::class)
abstract class AppDataBase : RoomDatabase() {

    abstract fun storeDao(): StoreDao
    abstract fun reagentInDao(): ReagentInDao
    abstract fun historyDao(): HistoryDao
    abstract fun messageDao(): MessageDao

    //反编译后相当于java的内部类,kotlin中称伴生对象  java中调AppdataBase.Companion.getInstance()
    companion object {
        @Volatile
        private var instance: AppDataBase? = null

        fun getInstance(context: Context): AppDataBase {
            return instance ?: synchronized(this) {
                instance ?: buildDatabase(context).also { instance = it }
            }
        }

        private fun buildDatabase(context: Context): AppDataBase {
            return Room.databaseBuilder(
                context.applicationContext, AppDataBase::class.java,
                DATABASE_NAME
            ).build()
        }
    }
}